package edu.sias.hyznxfapi.checkCompliance.checkVideo.utils.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sias.hyznxfapi.checkCompliance.checkAudioFre.Constants;
import edu.sias.hyznxfapi.checkCompliance.checkText.utils.MyUtil;

import java.util.Map;

public class CheckVideo {
    private static final String url = "https://audit.iflyaisol.com/audit/v2/video";
    private static final String queryUrl = "https://audit.iflyaisol.com/audit/v2/query";

    private static final String APPID = Constants.APPID;
    private static final String APISecret = Constants.APISecret;
    private static final String APIKey = Constants.APIKey;


    /**
     * 需要传递文件的url
     * @param audioUrl
     * @return
     * @throws Exception
     */
    public String VideoCheck(String audioUrl) throws Exception {
        // notify_url:请求回调的地址
        String json = "{\n" +
                "  \"video_list\": [\n" +
                "    {\n" +
                "      \"video_type\": \"" + audioUrl.substring(audioUrl.length() - 3) + "\",\n" +
                "      \"file_url\": \"" + audioUrl + "\",\n" +
                "      \"name\": \"" + "xxx" + audioUrl.substring(audioUrl.length() - 8) + "\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"notify_url\": \"http://wdfgdzx.top:9999/user/audio_video_callback?mark=myselfGive\"\n" +
                "}";
        Map<String, String> urlParams = MyUtil.getAuth(APPID, APIKey, APISecret);
        String returnResult = MyUtil.doPostJson(url, urlParams, json);
        String request_id = (String) JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("request_id");
        while (true) {
            String queryJson = "{\n" +
                    "  \"request_id\": \"" + request_id + "\"\n" +
                    "}";
            urlParams = MyUtil.getAuth(APPID, APIKey, APISecret);
            returnResult = MyUtil.doPostJson(queryUrl, urlParams, queryJson);
            Integer finishIndex = (Integer) JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("audit_status");
            if (finishIndex == 0) {
                System.out.println("待审核...");
            }
            if (finishIndex == 1) {
                System.out.println("审核中...");
            }
            if (finishIndex == 2) {
                System.out.println("审核完成");
                System.out.println(returnResult);
                break;
            }
            if (finishIndex == 4) {
                System.out.println("审核异常");
                System.out.println(returnResult);
            }
            Thread.sleep(5000);
        }
        JSONObject jsonObject = JSON.parseObject(returnResult);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        JSONArray resultListArray = dataObject.getJSONArray("result_list");
        String suggest = "";
        for (int i = 0; i < resultListArray.size(); i++) {
            String suggest1 = resultListArray.getJSONObject(i).getString("suggest");
            suggest = suggest1;
            System.out.println("suggest: " + suggest);
        }


        if (JSONObject.parseObject(JSONObject.parseObject(returnResult).get("data").toString()).get("audit_status").toString().equals("2")){
            return suggest;
            //pass：通过
            //review：疑似，建议人工复审
            //block：内容识别未通过
        }else {
            return "";
        }
    }


    public static void main(String[] args) {
        try {
            new CheckVideo().VideoCheck("https://edu-subject-1.oss-cn-beijing.aliyuncs.com/test/1.mp4");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
